package nuc.edu.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import nuc.edu.tlias.mapper.ClazzMapper;
import nuc.edu.tlias.pojo.Clazz;
import nuc.edu.tlias.pojo.PageResult;
import nuc.edu.tlias.service.ClazzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class ClazzServiceImpl implements ClazzService {
    private static final Logger log = LoggerFactory.getLogger(ClazzServiceImpl.class);
    @Autowired
    ClazzMapper clazzMapper;
    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }

    @Override
    public List<Clazz> findAll() {
        List<Clazz> list = clazzMapper.findAll();
        return list;
    }

    @Override
    public PageResult serchPage(String name ,LocalDate begin ,LocalDate end,Integer page ,Integer pageSize) {

        log.info("_____________________{},{},{},{},{}",name ,begin ,end,page , pageSize);

        PageHelper.startPage(page,pageSize);

        List<Clazz> list = clazzMapper.searchPage(name,begin,end);

        Page<Clazz> p = (Page<Clazz>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public Clazz findById(Integer id) {
        Clazz clazz = clazzMapper.findById(id);
        return clazz;
    }

    @Override
    public void upload(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.upload(clazz);
    }
}
